home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / c2latex.lha / c++2latex / getopt.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  16KB  |  566 lines

  1. * Getopt for GNU.
  2.    Copyright (C) 1987, 1989 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. * This version of `getopt' appears to the caller like standard Unix `getopt'
  19.    but it behaves differently for the user, since it allows the user
  20.    to intersperse the options with the other arguments.
  21.  
  22.    As `getopt' works, it permutes the elements of `argv' so that,
  23.    when it is done, all the options precede everything else.  Thus
  24.    all application programs are extended to handle flexible argument order.
  25.  
  26.    Setting the environment variable _POSIX_OPTION_ORDER disables permutation.
  27.    Then the behavior is completely standard.
  28.  
  29.    GNU application programs can use a third alternative mode in which
  30.    they can distinguish the relative order of options and other arguments.  */
  31.  
  32. include <stdio.h>
  33.  
  34. * If compiled with GNU C, use the built-in alloca */
  35. ifdef __GNUC__
  36. define alloca __builtin_alloca
  37. else /* not __GNUC__ */
  38. ifdef sparc
  39. include <alloca.h>
  40. else
  41. har *alloca ();
  42. endif
  43. endif /* not __GNUC__ */
  44.  
  45. ifdef USG
  46. define bcopy(s, d, l) memcpy((d), (s), (l))
  47. define index strchr
  48. endif
  49.  
  50. har *getenv ();
  51. har *index ();
  52. har *malloc ();
  53.  
  54. * For communication from `getopt' to the caller.
  55.    When `getopt' finds an option that takes an argument,
  56.    the argument value is returned here.
  57.    Also, when `ordering' is RETURN_IN_ORDER,
  58.    each non-option ARGV-element is returned here.  */
  59.  
  60. har *optarg = 0;
  61.  
  62. * Index in ARGV of the next element to be scanned.
  63.    This is used for communication to and from the caller
  64.    and for communication between successive calls to `getopt'.
  65.  
  66.    On entry to `getopt', zero means this is the first call; initialize.
  67.  
  68.    When `getopt' returns EOF, this is the index of the first of the
  69.    non-option elements that the caller should itself scan.
  70.  
  71.    Otherwise, `optind' communicates from one call to the next
  72.    how much of ARGV has been scanned so far.  */
  73.  
  74. nt optind = 0;
  75.  
  76. * The next char to be scanned in the option-element
  77.    in which the last option character we returned was found.
  78.    This allows us to pick up the scan where we left off.
  79.  
  80.    If this is zero, or a null string, it means resume the scan
  81.    by advancing to the next ARGV-element.  */
  82.  
  83. tatic char *nextchar;
  84.  
  85. * Callers store zero here to inhibit the error message
  86.    for unrecognized options.  */
  87.  
  88. nt opterr = 1;
  89.  
  90. * Describe how to deal with options that follow non-option ARGV-elements.
  91.  
  92.    If the caller did not specify anything,
  93.    the default is REQUIRE_ORDER if the environment variable
  94.    _POSIX_OPTION_ORDER is defined, PERMUTE otherwise.
  95.  
  96.    REQUIRE_ORDER means don't recognize them as options.
  97.    Stop option processing when the first non-option is seen.
  98.    This is what Unix does.
  99.  
  100.    PERMUTE is the default.  We permute the contents of ARGV as we scan,
  101.    so that eventually all the options are at the end.  This allows options
  102.    to be given in any order, even with programs that were not written to
  103.    expect this.
  104.  
  105.    RETURN_IN_ORDER is an option available to programs that were written
  106.    to expect options and other ARGV-elements in any order and that care about
  107.    the ordering of the two.  We describe each non-option ARGV-element
  108.    as if it were the argument of an option with character code one.
  109.    Using `-' as the first character of the list of option characters
  110.    requests this mode of operation.
  111.  
  112.    The special argument `--' forces an end of option-scanning regardless
  113.    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
  114.    `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
  115.  
  116. tatic enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering;
  117.  
  118. * Describe the long-named options requested by the application.
  119.    _GETOPT_LONG_OPTIONS is a vector of `struct option' terminated by an
  120.    element containing a name which is zero.
  121.    The field `has_arg' is 1 if the option takes an argument, 
  122.    2 if it takes an optional argument.  */
  123.  
  124. truct option
  125.  
  126.   char *name;
  127.   int has_arg;
  128.   int *flag;
  129.   int val;
  130. ;
  131.  
  132. truct option *_getopt_long_options;
  133.  
  134. nt _getopt_long_only = 0;
  135.  
  136. if 0
  137. * This is an ugly kludge.  Programs should use the opt_index argument
  138.    to getopt_long instead.  */
  139. * Name of long-named option actually found.  */
  140.  
  141. har *_getopt_option_name;
  142. endif
  143.  
  144. * Index in _GETOPT_LONG_OPTIONS of the long-named option actually found.
  145.    Only valid when a long-named option was found. */
  146.  
  147. nt option_index;
  148.  
  149. * Handle permutation of arguments.  */
  150.  
  151. * Describe the part of ARGV that contains non-options that have
  152.    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
  153.    `last_nonopt' is the index after the last of them.  */
  154.  
  155. tatic int first_nonopt;
  156. tatic int last_nonopt;
  157.  
  158. * Exchange two adjacent subsequences of ARGV.
  159.    One subsequence is elements [first_nonopt,last_nonopt)
  160.     which contains all the non-options that have been skipped so far.
  161.    The other is elements [last_nonopt,optind), which contains all
  162.     the options processed since those non-options were skipped.
  163.  
  164.    `first_nonopt' and `last_nonopt' are relocated so that they describe
  165.     the new indices of the non-options in ARGV after they are moved.  */
  166.  
  167. tatic void
  168. xchange (argv)
  169.      char **argv;
  170.  
  171.   int nonopts_size
  172.     = (last_nonopt - first_nonopt) * sizeof (char *);
  173.   char **temp = (char **) alloca (nonopts_size);
  174.  
  175.   /* Interchange the two blocks of data in argv.  */
  176.  
  177.   bcopy (&argv[first_nonopt], temp, nonopts_size);
  178.   bcopy (&argv[last_nonopt], &argv[first_nonopt],
  179.      (optind - last_nonopt) * sizeof (char *));
  180.   bcopy (temp, &argv[first_nonopt + optind - last_nonopt],
  181.      nonopts_size);
  182.  
  183.   /* Update records for the slots the non-options now occupy.  */
  184.  
  185.   first_nonopt += (optind - last_nonopt);
  186.   last_nonopt = optind;
  187.  
  188.  
  189. * Scan elements of ARGV (whose length is ARGC) for option characters
  190.    given in OPTSTRING.
  191.  
  192.    If an element of ARGV starts with '-', and is not exactly "-" or "--",
  193.    then it is an option element.  The characters of this element
  194.    (aside from the initial '-') are option characters.  If `getopt'
  195.    is called repeatedly, it returns successively each of the option characters
  196.    from each of the option elements.
  197.  
  198.    If `getopt' finds another option character, it returns that character,
  199.    updating `optind' and `nextchar' so that the next call to `getopt' can
  200.    resume the scan with the following option character or ARGV-element.
  201.  
  202.    If there are no more option characters, `getopt' returns `EOF'.
  203.    Then `optind' is the index in ARGV of the first ARGV-element
  204.    that is not an option.  (The ARGV-elements have been permuted
  205.    so that those that are not options now come last.)
  206.  
  207.    OPTSTRING is a string containing the legitimate option characters.
  208.    If an option character is seen that is not listed in OPTSTRING,
  209.    return '?' after printing an error message.  If you set `opterr' to
  210.    zero, the error message is suppressed but we still return '?'.
  211.  
  212.    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  213.    so the following text in the same ARGV-element, or the text of the following
  214.    ARGV-element, is returned in `optarg'.  Two colons mean an option that
  215.    wants an optional arg; if there is text in the current ARGV-element,
  216.    it is returned in `optarg', otherwise `optarg' is set to zero.
  217.  
  218.    If OPTSTRING starts with `-', it requests a different method of handling the
  219.    non-option ARGV-elements.  See the comments about RETURN_IN_ORDER, above.
  220.  
  221.    Long-named options begin with `+' instead of `-'.
  222.    Their names may be abbreviated as long as the abbreviation is unique
  223.    or is an exact match for some defined option.  If they have an
  224.    argument, it follows the option name in the same ARGV-element, separated
  225.    from the option name by a `=', or else the in next ARGV-element.
  226.    `getopt' returns 0 when it finds a long-named option.  */
  227.  
  228. nt
  229. etopt (argc, argv, optstring)
  230.      int argc;
  231.      char **argv;
  232.      char *optstring;
  233.  
  234.   optarg = 0;
  235.  
  236.   /* Initialize the internal data when the first call is made.
  237.      Start processing options with ARGV-element 1 (since ARGV-element 0
  238.      is the program name); the sequence of previously skipped
  239.      non-option ARGV-elements is empty.  */
  240.  
  241.   if (optind == 0)
  242.     {
  243.       first_nonopt = last_nonopt = optind = 1;
  244.  
  245.       nextchar = 0;
  246.  
  247.       /* Determine how to handle the ordering of options and nonoptions.  */
  248.  
  249.       if (optstring[0] == '-')
  250.     ordering = RETURN_IN_ORDER;
  251.       else if (getenv ("_POSIX_OPTION_ORDER") != 0)
  252.     ordering = REQUIRE_ORDER;
  253.       else
  254.     ordering = PERMUTE;
  255.     }
  256.  
  257.   if (nextchar == 0 || *nextchar == 0)
  258.     {
  259.       if (ordering == PERMUTE)
  260.     {
  261.       /* If we have just processed some options following some non-options,
  262.          exchange them so that the options come first.  */
  263.  
  264.       if (first_nonopt != last_nonopt && last_nonopt != optind)
  265.         exchange (argv);
  266.       else if (last_nonopt != optind)
  267.         first_nonopt = optind;
  268.  
  269.       /* Now skip any additional non-options
  270.          and extend the range of non-options previously skipped.  */
  271.  
  272.       while (optind < argc
  273.          && (argv[optind][0] != '-'
  274.              || argv[optind][1] == 0)
  275.          && (_getopt_long_options == 0
  276.              || argv[optind][0] != '+'
  277.              || argv[optind][1] == 0))
  278.         optind++;
  279.       last_nonopt = optind;
  280.     }
  281.  
  282.       /* Special ARGV-element `--' means premature end of options.
  283.      Skip it like a null option,
  284.      then exchange with previous non-options as if it were an option,
  285.      then skip everything else like a non-option.  */
  286.  
  287.       if (optind != argc && !strcmp (argv[optind], "--"))
  288.     {
  289.       optind++;
  290.  
  291.       if (first_nonopt != last_nonopt && last_nonopt != optind)
  292.         exchange (argv);
  293.       else if (first_nonopt == last_nonopt)
  294.         first_nonopt = optind;
  295.       last_nonopt = argc;
  296.  
  297.       optind = argc;
  298.     }
  299.  
  300.       /* If we have done all the ARGV-elements, stop the scan
  301.      and back over any non-options that we skipped and permuted.  */
  302.  
  303.       if (optind == argc)
  304.     {
  305.       /* Set the next-arg-index to point at the non-options
  306.          that we previously skipped, so the caller will digest them.  */
  307.       if (first_nonopt != last_nonopt)
  308.         optind = first_nonopt;
  309.       return EOF;
  310.     }
  311.      
  312.       /* If we have come to a non-option and did not permute it,
  313.      either stop the scan or describe it to the caller and pass it by.  */
  314.  
  315.       if ((argv[optind][0] != '-' || argv[optind][1] == 0)
  316.       && (_getopt_long_options == 0
  317.           || argv[optind][0] != '+' || argv[optind][1] == 0))
  318.     {
  319.       if (ordering == REQUIRE_ORDER)
  320.         return EOF;
  321.       optarg = argv[optind++];
  322. if 0
  323.       _getopt_option_name = 0;
  324. endif
  325.       return 1;
  326.     }
  327.  
  328.       /* We have found another option-ARGV-element.
  329.      Start decoding its characters.  */
  330.  
  331.       nextchar = argv[optind] + 1;
  332.     }
  333.  
  334.   if (_getopt_long_options != 0
  335.       && (argv[optind][0] == '+'
  336.       || (_getopt_long_only && argv[optind][0] == '-'))
  337.       )
  338.     {
  339.       struct option *p;
  340.       char *s = nextchar;
  341.       int exact = 0;
  342.       int ambig = 0;
  343.       struct option *pfound = 0;
  344.       int indfound;
  345.  
  346.       while (*s && *s != '=') s++;
  347.  
  348.       /* Test all options for either exact match or abbreviated matches.  */
  349.       for (p = _getopt_long_options, option_index = 0; p->name; 
  350.        p++, option_index++)
  351.     if (!strncmp (p->name, nextchar, s - nextchar))
  352.       {
  353.         if (s - nextchar == strlen (p->name))
  354.           {
  355.         /* Exact match found.  */
  356.         pfound = p;
  357.         indfound = option_index;
  358.         exact = 1;
  359.         break;
  360.           }
  361.         else if (pfound == 0)
  362.           {
  363.         /* First nonexact match found.  */
  364.         pfound = p;
  365.         indfound = option_index;
  366.           }
  367.         else
  368.           /* Second nonexact match found.  */
  369.           ambig = 1;
  370.       }
  371.  
  372.       if (ambig && !exact)
  373.     {
  374.       fprintf (stderr, "%s: option `%s' is ambiguous\n",
  375.            argv[0], argv[optind]);
  376.       nextchar += strlen (nextchar);               
  377.       return '?';
  378.     }
  379.  
  380.       if (pfound != 0)
  381.     {
  382.       option_index = indfound;
  383.       optind++;
  384.       if (*s)
  385.         {
  386.           if (pfound->has_arg > 0)
  387.         optarg = s + 1;
  388.           else
  389.         {
  390.           fprintf (stderr,
  391.                "%s: option `%c%s' doesn't allow an argument\n",
  392.                argv[0], argv[optind - 1][0], pfound->name);
  393.           nextchar += strlen (nextchar);               
  394.           return '?';
  395.         }
  396.         }
  397.       else if (pfound->has_arg)
  398.         {
  399.           if (optind < argc)
  400.         optarg = argv[optind++];
  401.           else if (pfound->has_arg != 2)
  402.         {
  403.           fprintf (stderr, "%s: option `%s' requires an argument\n",
  404.                argv[0], argv[optind - 1]);
  405.           nextchar += strlen (nextchar);           
  406.           return '?';
  407.         }
  408.         }
  409. if 0
  410.       _getopt_option_name = pfound->name;
  411. endif
  412.       nextchar += strlen (nextchar);
  413.       if (pfound->flag)
  414.         *(pfound->flag) = pfound->val;
  415.       return 0;
  416.     }
  417.       /* Can't find it as a long option.  If this is getopt_long_only,
  418.      and the option starts with '-' and is a valid short
  419.      option, then interpret it as a short option.  Otherwise it's
  420.      an error.  */
  421.       if (_getopt_long_only == 0 || argv[optind][0] == '+' ||
  422.       index (optstring, *nextchar) == 0)
  423.     {
  424.       if (opterr != 0)
  425.         fprintf (stderr, "%s: unrecognized option `%c%s'\n",
  426.              argv[0], argv[optind][0], nextchar);
  427.       nextchar += strlen (nextchar);           
  428.       return '?';
  429.     }
  430.     }
  431.  
  432.   /* Look at and handle the next option-character.  */
  433.  
  434.   {
  435.     char c = *nextchar++;
  436.     char *temp = index (optstring, c);
  437.  
  438.     /* Increment `optind' when we start to process its last character.  */
  439.     if (*nextchar == 0)
  440.       optind++;
  441.  
  442.     if (temp == 0 || c == ':')
  443.       {
  444.     if (opterr != 0)
  445.       {
  446.         if (c < 040 || c >= 0177)
  447.           fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
  448.                argv[0], c);
  449.         else
  450.           fprintf (stderr, "%s: unrecognized option `-%c'\n",
  451.                argv[0], c);
  452.       }
  453.     return '?';
  454.       }
  455.     if (temp[1] == ':')
  456.       {
  457.     if (temp[2] == ':')
  458.       {
  459.         /* This is an option that accepts an argument optionally.  */
  460.         if (*nextchar != 0)
  461.           {
  462.             optarg = nextchar;
  463.         optind++;
  464.           }
  465.         else
  466.           optarg = 0;
  467.         nextchar = 0;
  468.       }
  469.     else
  470.       {
  471.         /* This is an option that requires an argument.  */
  472.         if (*nextchar != 0)
  473.           {
  474.         optarg = nextchar;
  475.         /* If we end this ARGV-element by taking the rest as an arg,
  476.            we must advance to the next element now.  */
  477.         optind++;
  478.           }
  479.         else if (optind == argc)
  480.           {
  481.         if (opterr != 0)
  482.           fprintf (stderr, "%s: option `-%c' requires an argument\n",
  483.                argv[0], c);
  484.         c = '?';
  485.           }
  486.         else
  487.           /* We already incremented `optind' once;
  488.          increment it again when taking next ARGV-elt as argument.  */
  489.           optarg = argv[optind++];
  490.         nextchar = 0;
  491.       }
  492.       }
  493.     return c;
  494.   }
  495.  
  496.  
  497. ifdef TEST
  498.  
  499. * Compile with -DTEST to make an executable for use in testing
  500.    the above definition of `getopt'.  */
  501.  
  502. nt
  503. ain (argc, argv)
  504.      int argc;
  505.      char **argv;
  506.  
  507.   char c;
  508.   int digit_optind = 0;
  509.  
  510.   while (1)
  511.     {
  512.       int this_option_optind = optind;
  513.       if ((c = getopt (argc, argv, "abc:d:0123456789")) == EOF)
  514.     break;
  515.  
  516.       switch (c)
  517.     {
  518.     case '0':
  519.     case '1':
  520.     case '2':
  521.     case '3':
  522.     case '4':
  523.     case '5':
  524.     case '6':
  525.     case '7':
  526.     case '8':
  527.     case '9':
  528.       if (digit_optind != 0 && digit_optind != this_option_optind)
  529.         printf ("digits occur in two different argv-elements.\n");
  530.       digit_optind = this_option_optind;
  531.       printf ("option %c\n", c);
  532.       break;
  533.  
  534.     case 'a':
  535.       printf ("option a\n");
  536.       break;
  537.  
  538.     case 'b':
  539.       printf ("option b\n");
  540.       break;
  541.  
  542.     case 'c':
  543.       printf ("option c with value `%s'\n", optarg);
  544.       break;
  545.  
  546.     case '?':
  547.       break;
  548.  
  549.     default:
  550.       printf ("?? getopt returned character code 0%o ??\n", c);
  551.     }
  552.     }
  553.  
  554.   if (optind < argc)
  555.     {
  556.       printf ("non-option ARGV-elements: ");
  557.       while (optind < argc)
  558.     printf ("%s ", argv[optind++]);
  559.       printf ("\n");
  560.     }
  561.  
  562.   return 0;
  563.  
  564.  
  565. endif /* TEST */
  566.